package com.uestc.utils;

import javafx.geometry.Insets;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.GridPane;

import java.util.Optional;

public class DialogUtil {
    private static String password;

    public static void showPasswordDialog(String title, String txtFieldName) {
        Dialog<String> dialog = new Dialog<>();
        dialog.setTitle(title);

        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 150, 10, 10));

        PasswordField passwordField = new PasswordField();
        grid.add(new Label(txtFieldName + ":"), 0, 0);
        grid.add(passwordField, 1, 0);

        dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
        dialog.getDialogPane().lookupButton(ButtonType.OK).setDisable(true);

        passwordField.textProperty().addListener((observable, oldValue, newValue) -> {
            dialog.getDialogPane().lookupButton(ButtonType.OK).setDisable(newValue.trim().isEmpty());
            password = passwordField.getText();
        });

        dialog.getDialogPane().setContent(grid); // 设置 GridPane 为对话框内容
        dialog.showAndWait();
    }

    public static String getPassword(){
        return password;
    }
}